import util from '@/libs/util'
export default {
  data () {
    return {
      search: {},
      active: true,
      pagination: {
        current: 1,
        size: 25,
        total: 1
      },
      searchDialogConf: { dialogVisible: false },
      searchFormItems: [],
      searchFields: []
    }
  },
  watch: {
    formItemSetting () {
      this.initSearchForm()
    }
  },
  computed: {
    searchColumns () {
      return this.formItemSetting.map(e => { return { key: e.prop, label: e.title } })
    }
  },
  created () {
    this.searchFields = this.formItemSetting.filter(e => e.general === true).map(e => { return e.prop })
  },
  mounted () {

  },
  methods: {
    searchSettingChange (val) {
      if (val === null) {
        val = this.formItemSetting.filter(e => e.general === true).map(e => { return e.prop })
      }
      this.searchFields = val
      this.$nextTick(() => {
        this.initSearchForm()
      })
    },
    getTableData () {
      return this.$refs.table.getTableData()
    },
    // 刷新
    handleFetchTableData () {
      this.$refs.table.refresh(this.search)
    },
    // 搜索
    handleSearchTableData () {
      this.pagination.current = 1
      this.$refs.table.refresh(this.search)
    },
    handleRestTableData () {
      this.search = {}
      this.handleFetchTableData()
    },
    // 设置
    handleSettingTable () {
      this.$refs.table.handleOpenSetting()
    },
    // 搜索设置
    handleSearchSetting () {
      this.searchDialogConf.dialogVisible = true
      this.searchDialogConf.columns = this.searchColumns
    },
    // 初始化搜索表单
    initSearchForm () {
      if (this.searchFields !== undefined && this.searchFields.length) {
        this.searchFormItems = this.formItemSetting.filter(e => this.searchFields.indexOf(e.prop) > -1)
      } else {
        this.searchFormItems = this.$_.cloneDeep(this.formItemSetting)
      }
      this.searchFormItems.push({
        title: '操作',
        visible: true,
        name: 'button-group',
        options: [
          {
            label: '搜索',
            type: 'primary',
            click: () => {
              if (this.customFetchTableData) {
                this.customFetchTableData()
                return
              }
              this.handleFetchTableData()
            }
          },
          {
            label: '重置',
            click: () => {
              if (this.customResetTableData) {
                this.customResetTableData()
                return
              }
              this.handleRestTableData()
            }
          }
        ]
      })
    },
    // 分页选择
    handlePageChange ({ currentPage, pageSize }) {
      this.pagination.current = currentPage
      this.pagination.size = pageSize
      this.handleFetchTableData()
    },
    // 全选
    handleAllSelection () {
      this.$refs.table.setAllCheckboxRow(true)
    },
    // 反选
    handleInvertSelection () {
      this.$refs.table.invertSelectionRow()
    },
    // 全不选
    handleAllNoneSelection () {
      this.$refs.table.clearCheckboxRow()
    },
    // 删除
    handleDeleteTable () {
      this.$refs.table.deleteSelectionRow()
    },
    // 改变状态
    handleChangeStatus ({ row }) {
      if (this.api) {
        let param = { status: row.status }
        if (this.api.status) {
          this.api.status(row.id, param).then(res => {
            var { code } = res
            if (code === 200) {
              this.$message.success('修改成功')
            } else {
              row.status = !row.status
              this.$message.success('修改失败')
            }
          })
        } else if (this.api.modify) {
          this.api.modify(row.id, param).then(res => {
            var { code } = res
            if (code === 200) {
              this.$message.success('修改成功')
            } else {
              row.status = !row.status
              this.$message.success('修改失败')
            }
          })
        }
      }
    },
    // 下载模板
    handleDownloadTemplate () {
      if (this.api && this.api.downloadTemplate) {
        this.api.downloadTemplate().then(res => {
          util.downloadFile(res, 'template', 'xlsx')
        })
      }
    },
    // 导入数据
    handleImportData (file) {
      if (this.api && this.api.importExcel && file) {
        this.api.importExcel(file).then(res => {
          var { code, message } = res
          if (code === 200) {
            this.handleRestTableData()
            this.$message.success('导入成功')
          } else {
            if (message !== null && message !== '') {
              this.$message.error(message)
            } else {
              this.$message.error('导入失败')
            }
          }
        })
      }
    }
  }
}
